﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsJobTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// JobForward Business Object
    /// </summary>

    public class JobForward : BOBase
    {

        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected JobForward() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the Forward.
        /// </summary>
        public int ForwardID
        {
            get { return _forwardID; }
            set
            {
                if (_forwardID != value)
                {
                    if (OnPropertyChanging("ForwardID", _forwardID, value))
                    {
                        int oldValue = _forwardID;
                        _forwardID = value;

                        OnPropertyChanged("ForwardID", oldValue, value);
                    }
                }
            }
        }  private int _forwardID = -1;

        /// <summary>
        /// JobID_RKJobs.
        /// </summary>
        public string JobID_RKJobs
        {
            get { return _jobID_RKJobs; }
            set
            {
                if (_jobID_RKJobs != value)
                {
                    if (OnPropertyChanging("JobID_RKJobs", _jobID_RKJobs, value))
                    {
                        string oldValue = _jobID_RKJobs;
                        _jobID_RKJobs = value;
                        OnPropertyChanged("JobID_RKJobs", oldValue, value);
                    }
                }
            }
        }  private string _jobID_RKJobs = null;


        /// <summary>
        /// PKID of the ForwardedBy_RKRegisteredUserID.
        /// </summary>
        public int ForwardedBy_RKRegisteredUserID
        {
            get { return _forwardedBy_RKRegisteredUserID; }
            set
            {
                if (_forwardedBy_RKRegisteredUserID != value)
                {
                    if (OnPropertyChanging("ForwardedBy_RKRegisteredUserID", _forwardedBy_RKRegisteredUserID, value))
                    {
                        int oldValue = _forwardedBy_RKRegisteredUserID;
                        _forwardedBy_RKRegisteredUserID = value;

                        OnPropertyChanged("ForwardedBy_RKRegisteredUserID", oldValue, value);
                    }
                }
            }
        }  private int _forwardedBy_RKRegisteredUserID = -1;



        /// <summary>
        /// FriendsName.
        /// </summary>
        public string FriendsName
        {
            get { return _friendsName; }
            set
            {
                if (_friendsName != value)
                {
                    if (OnPropertyChanging("FriendsName", _friendsName, value))
                    {
                        string oldValue = _friendsName;
                        _friendsName = value;
                        OnPropertyChanged("FriendsName", oldValue, value);
                    }
                }
            }
        }  private string _friendsName = null;


        /// <summary>
        /// FriendsEmailAddress.
        /// </summary>
        public string FriendsEmailAddress
        {
            get { return _friendsEmailAddress; }
            set
            {
                if (_friendsEmailAddress != value)
                {
                    if (OnPropertyChanging("FriendsEmailAddress", _friendsEmailAddress, value))
                    {
                        string oldValue = _friendsEmailAddress;
                        _friendsEmailAddress = value;
                        OnPropertyChanged("FriendsEmailAddress", oldValue, value);
                    }
                }
            }
        }  private string _friendsEmailAddress = null;



        /// <summary>
        /// MessageText.
        /// </summary>
        public string MessageText
        {
            get { return _messageText; }
            set
            {
                if (_messageText != value)
                {
                    if (OnPropertyChanging("MessageText", _messageText, value))
                    {
                        string oldValue = _messageText;
                        _messageText = value;
                        OnPropertyChanged("MessageText", oldValue, value);
                    }
                }
            }
        }  private string _messageText = null;



        /// <summary>
        /// Date/time at which the Job was forwarded.
        /// </summary>
        public DateTime? ForwardedDate
        {
            get { return _forwardedDate; }
            set
            {
                if (_forwardedDate != value)
                {
                    if (OnPropertyChanging("ForwardedDate", _forwardedDate, value))
                    {
                        DateTime? oldValue = _forwardedDate;
                        _forwardedDate = value;
                        OnPropertyChanged("ForwardedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _forwardedDate = null;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobForward object.
        /// </summary>
        public static JobForward CreateNew()
        {
            JobForward obj = new JobForward();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.ForwardedDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static JobForward CreateNew(JobForwardCollection collection)
        {
            JobForward obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (JobForward jobforward in collection)
            {
                if (jobforward.ForwardID < minVal)
                    minVal = jobforward.ForwardID;
            }
            obj.ForwardID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllJobForwardByUserID

        /// <summary>
        /// Returns a collection of all JobForward.
        /// </summary>
        public static JobForwardCollection GetAllJobForwardByUserID(int userid)
        {
            JobForwardsTableAdapter adapter = new JobForwardsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsJob.JobForwardsDataTable tbl = adapter.GetData(userid);

            JobForwardCollection list = new JobForwardCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsJob.JobForwardsRow row in tbl.Rows)
                {
                    JobForward jobforward = new JobForward();
                    RelationalToObject(jobforward, row);
                    list.Add(jobforward);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified JobForward.
        /// </summary>
        public static void Update(JobForward obj)
        {
            bool updateJobForward = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateJobForward = (obj.ObjState != ObjectState.Unchanged);

                if (!updateJobForward)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsJob.JobForwardsDataTable tbl = null;
            if (updateJobForward)
            {
                JobForwardsTableAdapter adapter = new JobForwardsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsJob.JobForwardsDataTable();
                dsJob.JobForwardsRow row = tbl.NewJobForwardsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (ForwardID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("JobForwardID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("JobForward: ForwardID={0}, FriendName={1}, {2}", ForwardID, FriendsName, FriendsEmailAddress));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more JobForward objects.
    /// </summary>

    public class JobForwardCollection : List<JobForward>
    {
        #region Types

        public class UserComparer : IComparer<JobForward>
        {
            public int Compare(JobForward jf1, JobForward jf2)
            {
                return (String.Compare(jf1.ForwardID.ToString(), jf2.ForwardID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindByJobForwardID

        public JobForward FindByJobForwardID(int JobForwardID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].ForwardID == JobForwardID)
                    return (this[ndx]);

            return (null);
        }

        #endregion


        #endregion
    }
}
